package util

import (
	"crypto/md5"
	"strconv"
	"time"

	// "io"
	"fmt"
	"math/rand"
)

var letter = []rune("abcdefghijklmnopqrstuvwxyz_-+=$@1234567890*%^(A)BCDEFGHIJKLMNOPQRSTUVWXYZ@")

// 盐
func Salt(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letter[rand.Intn(len(letter))]
	}
	return string(b)
}

func Passwd(n int, paswd map[string]string) string {

	if n == 0 {
		n = 64
	}
	pass := Md5(paswd["passwd"] + paswd["salt"] + paswd["passwd"])
	return Md5(paswd["salt"] + pass + paswd["salt"])
}

// md5加密
func Md5(str string) string {
	if str == "" {
		return "0"
	}
	has := md5.Sum([]byte(str))
	md5str := fmt.Sprintf("%x", has)
	return md5str
}

// 随机加密串
func RandStr() string {
	var strTime string = strconv.FormatInt(time.Now().UnixMilli(), 10)
	result := ""
	min := 1
	max := 60
	randomIntBetween := rand.Intn(max-min+1) + min
	for i := 0; i < randomIntBetween; i++ {
		result += string(letter[rand.Intn(len(letter))])
	}
	return strTime + result
}
